#pragma once

//定制协议
#include <iostream>
#include <jsoncpp/json/json.h>

typedef class Request
{
public:
    int x;
    int y;
    char op;

public:
    Request() {}
    Request(int _x, int _y, char _op) : x(_x), y(_y), op(_op) {}
    ~Request() {}
}Request_t;

typedef class Respose
{
public:
    int code;
    int res;
public:
    Respose():code(0){}
    Respose(int _res):res(_res), code(0)
    {}
    ~Respose(){}
}Response_t;

//序列化过程
std::string SerializeRequest(const Request_t& req)
{
    Json::Value root;
    root["dataX"] = req.x;
    root["dataY"] = req.y;
    root["dataOP"] = req.op;

    Json::FastWriter fast_writer;
    Json::StyledWriter styled_writer;
    std::string json_string1 = fast_writer.write(root);
    std::string json_string2 = styled_writer.write(root);
    //打印一下json串
    std::cout << json_string1 << std::endl;
    std::cout << json_string2 << std::endl;
    return json_string1;
}

std::string SerializeResponse(const Response_t rep)
{
    //可以填充任何对象，json是一种以KV键对序列化的形式
    Json::Value root;
    root["code"] = rep.code;
    root["dataResult"] = rep.res;

    //区别FastWriter、StyledWriter
    Json::FastWriter writer;
    //Json::StyledWriter writer;
    std::string json_string = writer.write(root);
    return json_string;
}

//反序列化
void DeserializeRequest(const std::string& json_string, Request_t& out)
{
    Json::Reader reader;
    Json::Value root;

    reader.parse(json_string, root);
    out.x = root["dataX"].asInt();
    out.y = root["dataY"].asInt();
    out.op = (char)root["dataOP"].asInt();
}

void DeserializeResponse(const std::string& json_string, Response_t& out)
{
    Json::Reader reader;
    Json::Value root;

    reader.parse(json_string, root);
    out.res = root["dataResult"].asInt();
    out.code = root["code"].asInt();
}